﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.GenericMetadata;
using SdmxMl.Manager;
using SdmxMl.Common;
using SdmxMl.Structure;

namespace SdmxMl.UC
{
    public partial class UcMetadataStructureRef : UserControl
    {
        private ISdmxArtefactRef metaSchemeRef;
        private MetadataSetType metaSetType;
        private bool onBinding;
        private ArtefactList alMetadataStructureDefinitionsList = null;
        private MetadataStructureDefinitionType msd = null;
        private ReportStructureType r = null;

        public bool IsFinal { get; set; }

     //   public event System.EventHandler ReferenceUpdated;

        public UcMetadataStructureRef()
        {
            InitializeComponent();
        }
        public void BindRef(MetadataSetType meta)
        {
            onBinding = true;
            metaSetType = meta;
            this.metaSchemeRef = metaSetType.MetadataStructureRef;

            if (meta == null)
            {
                textBoxConceptSchemeRefName.Text = string.Empty;
                textBoxAgency.Text = string.Empty;
                multiColumnMetaSchemeRef.Text = null;
                multiColumnMetaSchemeRef.DataSource = null;
                multiColumnReportRef.DataSource = null;
                multiColumnReportRef.Text = null;
            }
            else
            {
                alMetadataStructureDefinitionsList = ReferenceManager.GetArtefactList(SdmxArtefactType.MetadataStructureDefinitions);
                msd = ReferenceManager.LocateMetadataStructureDefinition(metaSchemeRef);
                r = ReferenceManager.LocateReportStructure(metaSchemeRef, metaSetType.ReportRef);


                string MetadataStructureName = msd == null ? string.Empty : msd.Name.ToString();

                multiColumnMetaSchemeRef.DataSource = alMetadataStructureDefinitionsList.GetTableRepresentation();
                multiColumnMetaSchemeRef.ColumnWidths = multiColumnMetaSchemeRef.Width.ToString() + ";" + (textBoxConceptSchemeRefName.Width + 20).ToString() + ":" + textBoxAgency.Width.ToString();
                if (msd != null)
                    multiColumnMetaSchemeRef.Text = msd.Id;
                else
                    multiColumnMetaSchemeRef.Text = metaSchemeRef.Id;

                multiColumnMetaSchemeRef.Enabled = IsFinal == false;
                if (string.IsNullOrEmpty(metaSchemeRef.Id))
                {
                    textBoxConceptSchemeRefName.Text = string.Empty;
                    textBoxAgency.Text = string.Empty;
                    multiColumnMetaSchemeRef.Text = null;

                }
                else
                {
                    textBoxConceptSchemeRefName.Text = metaSchemeRef.Version + " - " + MetadataStructureName;
                    textBoxAgency.Text = metaSchemeRef.AgencyId;
                }

                BindMetaReport(msd, metaSetType.ReportRef);
                onBinding = false;
            }

        }

        private void BindMetaReport(MetadataStructureDefinitionType msd, string reportRefId)
        {
            multiColumnReportRef.Enabled = IsFinal == false;
            r = null;
            if (msd != null)
                r = msd.ReportStructureList.LocateItem(reportRefId);

            string listConceptId = r == null ? string.Empty : r.Name.ToString();
            if (r != null)
            {
                multiColumnReportRef.DataSource =  msd.ReportStructureList.GetTableRepresentation();
                // After source was nulled, the displayMember is cleared, so redefine it
                multiColumnReportRef.DisplayMember = "ReportRef";
                multiColumnReportRef.ColumnWidths = multiColumnReportRef.Width.ToString() + ";" + (textBoxConceptName.Width / 2).ToString() + ";" + (textBoxConceptName.Width / 2).ToString();
                multiColumnReportRef.Text = r.Id;
                metaSetType.ReportRef = r.Id;

            }
            else
            {
                multiColumnReportRef.Text = reportRefId;
                multiColumnReportRef.DataSource = null;
                textBoxConceptName.Text = string.Empty;
            }

            buttonErrorConceptRef.Visible = msd == null || r == null;

        }


        private void textBoxConceptSchemeRefName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (onBinding)
                return;

            DataTable dt = multiColumnMetaSchemeRef.DataSource as DataTable;
            if (dt != null && multiColumnMetaSchemeRef.SelectedIndex >= 0)
            {

                msd = alMetadataStructureDefinitionsList[multiColumnMetaSchemeRef.SelectedIndex] as MetadataStructureDefinitionType;
                textBoxConceptSchemeRefName.Text = msd.Version + " - " + msd.Name.ToString();
                textBoxAgency.Text = msd.AgencyId;
                metaSchemeRef.Id = msd.Id;
                metaSchemeRef.Version = msd.Version;
                metaSchemeRef.AgencyId = msd.AgencyId;
                metaSetType.ReportRef = msd.ReportStructureList.Count > 0 ? msd.ReportStructureList[0].Id : string.Empty;

                BindMetaReport(msd, metaSetType.ReportRef);
            }
            else
            {
                textBoxConceptSchemeRefName.Text = string.Empty;
                textBoxAgency.Text = string.Empty;
            }

        }

        private void multiColumnReportRef_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (onBinding)
                return;

            DataTable dt = multiColumnReportRef.DataSource as DataTable;
            if (dt != null && multiColumnReportRef.SelectedIndex >= 0)
            {
                metaSetType.ReportRef = dt.Rows[multiColumnReportRef.SelectedIndex][0].ToString();
                multiColumnReportRef.Text = metaSetType.ReportRef;
            }

        }

        private void buttonNavigate_Click(object sender, EventArgs e)
        {
            if (metaSchemeRef != null)
            {
                try
                {
                    ArtefactRefTyped art = new ArtefactRefTyped(SdmxArtefactType.MetadataStructureDefinitions, metaSchemeRef.FullIdent);

                    // If already in memory, navigate otherwhile get from registry
                    if (NavigationManager.LocatenavigateProject(art.Type, art.FullIdent, null) == null)
                        NavigationManager.NavigateWithRegistry(art.Type, art.FullIdent, true, null);
                }
                catch { }
            }


        }

        private void buttonNavigateReport_Click(object sender, EventArgs e)
        {
            if (r != null)
                NavigationManager.RequestNavigation(r);
        }



    }
}
